<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:system="clr-namespace:System;assembly=mscorlib"
  xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
  xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf"
  xmlns:dentalConverters="clr-namespace:DentalConfig.Converters;assembly=DentalConfig"
  xmlns:dgh="clr-namespace:DentalGUIHelper;assembly=DentalConfig">

  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="../Brushes/MaterialDesign/MaterialDesignTheme.Theme.xaml"/>
    <ResourceDictionary Source="../NamedStyles/MaterialDesignTheme.NamedTextBlock.xaml"/>
  </ResourceDictionary.MergedDictionaries>

  <dentalConverters:ToUpperConverter x:Key="ToUpperConverter"/>

  <Style x:Key="FocusVisual">
    <Setter Property="Control.Template">
      <Setter.Value>
        <ControlTemplate>
          <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!-- Style for the left / right arrows used to scroll through the available tabs (if there are more that fit in one line) -->
  <!-- TODO: Disable Arrow if we can't scroll any further -->
  <Style x:Key="ScrollBarRepeatButton" TargetType="{x:Type RepeatButton}">
    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type wpf:ColorZone}}, Path=(BorderBrush)}"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Opacity" Value="0.5"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type RepeatButton}">
          <Border Background="Transparent" Height="18" Width="18" BorderThickness="0" VerticalAlignment="Bottom">
            <Path Data="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" Fill="{TemplateBinding Foreground}"/>
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Opacity" Value="1"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
              <Setter Property="Foreground" Value="{DynamicResource SecondaryAccentBrush}"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!-- =============================================== -->
  <!-- HorizontalScrollBarTemplate -->
  <!-- Adds a left and right arrow in the TabItem line -->
  <!-- =============================================== -->
  <ControlTemplate x:Key="HorizontalScrollBarTemplate" TargetType="{x:Type ScrollBar}">
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition MaxWidth="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}}"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition MaxWidth="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}}"/>
      </Grid.ColumnDefinitions>
      <RepeatButton Style="{StaticResource ScrollBarRepeatButton}" Command="ScrollBar.PageLeftCommand" Content="M 8 15 L 8 3 L 2 9 Z" Grid.Column="0"/>
      <RepeatButton Style="{StaticResource ScrollBarRepeatButton}" Command="ScrollBar.PageRightCommand" Content="M 9 15 L 9 3 L 15 9 Z" Grid.Column="2"/>
    </Grid>
  </ControlTemplate>

  <!-- ============================= -->
  <!-- ScrollViewerTemplate Template -->
  <!-- This ensures that all Tabs are placed in a single line -->
  <!-- ============================= -->
  <ControlTemplate x:Key="ScrollViewerTemplate" TargetType="{x:Type ScrollViewer}">
    <Grid>
      <ScrollBar
        x:Name="PART_HorizontalScrollBar"
        Height="14"
        Cursor="Arrow"
        Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
        Orientation="Horizontal"
        ViewportSize="{TemplateBinding ViewportWidth}"
        Maximum="{TemplateBinding ScrollableWidth}"
        Minimum="0"
        Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
        AutomationProperties.AutomationId="HorizontalScrollBar"
        Template="{DynamicResource HorizontalScrollBarTemplate}"/>
      <ScrollContentPresenter
        x:Name="PART_ScrollContentPresenter"
        Margin="5,0,5,0"
        Content="{TemplateBinding Content}"
        ContentTemplate="{TemplateBinding ContentTemplate}"
        CanContentScroll="True"
        CanHorizontallyScroll="False"
        CanVerticallyScroll="False"/>
    </Grid>
    <ControlTemplate.Triggers>
      <Trigger Property="ComputedHorizontalScrollBarVisibility" Value="Visible">
        <Setter TargetName="PART_ScrollContentPresenter" Property="Margin" Value="15,0"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>

  <!-- ==================== -->
  <!-- TabControl Style -->
  <!-- ==================== -->
  <Style x:Key="MaterialDesignTabControl" TargetType="{x:Type TabControl}">
    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Padding" Value="12,8"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TabControl}">
          <Grid KeyboardNavigation.TabNavigation="Local">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="0"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto"/>
              <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ScrollViewer VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" Template="{DynamicResource ScrollViewerTemplate}">
              <DockPanel>
                <StackPanel x:Name="HeaderPanel" DockPanel.Dock="Left" Orientation="Horizontal" IsItemsHost="True" Panel.ZIndex="1" KeyboardNavigation.TabIndex="1"/>
              </DockPanel>
            </ScrollViewer>
            <ContentPresenter
              x:Name="PART_SelectedContentHost"
              Grid.Column="0"
              Grid.Row="1"
              ContentTemplate="{TemplateBinding SelectedContentTemplate}"
              Content="{TemplateBinding SelectedContent}"
              ContentStringFormat="{TemplateBinding SelectedContentStringFormat}"
              ContentSource="SelectedContent"
              Margin="{TemplateBinding Padding}"
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!-- ================= -->
  <!-- TabItem Style -->
  <!-- ================= -->
  <Style x:Key="MaterialDesignTabItem" TargetType="{x:Type TabItem}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Padding" Value="8,0"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{DynamicResource SecondaryAccentBrush}"/>
    <Setter Property="TextBlock.FontFamily" Value="{DynamicResource ContentFontFamily}"/>
    <Setter Property="TextBlock.FontSize" Value="{DynamicResource NormalFontSize}"/>
    <Setter Property="dgh:TabService.AutoBringIntoView" Value="{Binding IsSelected, RelativeSource={RelativeSource Self}}" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="TabItem">
          <Grid SnapsToDevicePixels="true">
            <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
              <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <wpf:Ripple
              Tag="{Binding Path=Header, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"
              Content="{Binding Path=Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource ToUpperConverter}}"
              ContentTemplate="{TemplateBinding ContentTemplate}"
              Focusable="False"
              TextBlock.FontFamily="{DynamicResource ButtonFontFamily}"
              TextBlock.FontSize="{DynamicResource SmallSubHeaderFontSize}"
              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
              RecognizesAccessKey="False"
              Padding="{TemplateBinding Padding}"
              x:Name="contentPresenter"
              Opacity=".8"
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
              <wpf:Ripple.Resources>
                <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource UpperTextBlock}"/>
                <system:Double x:Key="DisabledOpacity">1</system:Double><!--TextBlock disabled opacity should not be applied on top of HintOpacity-->
              </wpf:Ripple.Resources>
            </wpf:Ripple>
            <Border x:Name="SelectionHighlightBorder" Background="{TemplateBinding BorderBrush}" Height="4" Margin="0,2,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Grid.Row="1" RenderTransformOrigin="0.5,1">
              <Border.RenderTransform>
                <TransformGroup>
                  <ScaleTransform x:Name="ScaleTransform" ScaleX="0" ScaleY="1" />
                </TransformGroup>
              </Border.RenderTransform>
            </Border>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter TargetName="contentPresenter" Property="Opacity" Value="1"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="true">
              <Setter TargetName="contentPresenter" Property="Opacity" Value="1"/>
              <Trigger.EnterActions>
                  <BeginStoryboard>
                      <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="ScaleX" Storyboard.TargetName="ScaleTransform" From="0" To="1" Duration="0:0:0.3">
                          <DoubleAnimation.EasingFunction>
                            <SineEase EasingMode="EaseOut" />
                          </DoubleAnimation.EasingFunction>
                        </DoubleAnimation>
                      </Storyboard>
                  </BeginStoryboard>
              </Trigger.EnterActions>
              <Trigger.ExitActions>
                  <BeginStoryboard>
                      <Storyboard>
                          <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="ScaleX" Storyboard.TargetName="ScaleTransform" From="1" To="0" Duration="0:0:0.3">
                              <DoubleAnimation.EasingFunction>
                                <SineEase EasingMode="EaseOut" />
                              </DoubleAnimation.EasingFunction>
                            </DoubleAnimation>
                          </Storyboard>
                      </Storyboard>
                  </BeginStoryboard>
              </Trigger.ExitActions>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
              <Setter Property="Opacity" Value="{StaticResource DisabledOpacity}"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="MaterialDesignGroupBoxTabItem" BasedOn="{StaticResource MaterialDesignTabItem}" TargetType="{x:Type TabItem}">
    <Setter Property="Padding" Value="8,4,8,0"/>
    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground), FallbackValue={StaticResource MaterialDesignBody}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{DynamicResource TabSelectionUnderline}"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="TabItem">
          <Grid SnapsToDevicePixels="true">
            <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
              <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <wpf:ColorZone Background="{DynamicResource PrimaryHueMidLightBrush}" Foreground="{DynamicResource PrimaryHueMidLightForegroundBrush}">
              <wpf:Ripple
                Tag="{Binding Path=Header, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"
                Content="{Binding Path=Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource ToUpperConverter}}"
                ContentTemplate="{TemplateBinding ContentTemplate}"
                Focusable="False"
                TextBlock.FontFamily="{DynamicResource ButtonFontFamily}"
                TextBlock.FontSize="{DynamicResource SmallSubHeaderFontSize}"
                HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                RecognizesAccessKey="False"
                Padding="{TemplateBinding Padding}"
                x:Name="contentPresenter"
                Opacity=".5"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                <wpf:Ripple.Resources>
                  <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource UpperTextBlock}"/>
                  <system:Double x:Key="DisabledOpacity">1</system:Double><!--TextBlock disabled opacity should not be applied on top of HintOpacity-->
                </wpf:Ripple.Resources>
              </wpf:Ripple>
            </wpf:ColorZone>
            <Border x:Name="SelectionHighlightBorder" Background="{TemplateBinding BorderBrush}" Height="5" Margin="0,3,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Grid.Row="1" RenderTransformOrigin="0.5,1">
              <Border.RenderTransform>
                <TransformGroup>
                  <ScaleTransform x:Name="ScaleTransform" ScaleX="0" ScaleY="1" />
                </TransformGroup>
              </Border.RenderTransform>
            </Border>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter TargetName="contentPresenter" Property="Opacity" Value="1"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="true">
              <Setter TargetName="contentPresenter" Property="Opacity" Value="1"/>
              <Trigger.EnterActions>
                  <BeginStoryboard>
                      <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="ScaleX" Storyboard.TargetName="ScaleTransform" From="0" To="1" Duration="0:0:0.3">
                          <DoubleAnimation.EasingFunction>
                            <SineEase EasingMode="EaseOut" />
                          </DoubleAnimation.EasingFunction>
                        </DoubleAnimation>
                      </Storyboard>
                  </BeginStoryboard>
              </Trigger.EnterActions>
              <Trigger.ExitActions>
                  <BeginStoryboard>
                      <Storyboard>
                          <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="ScaleX" Storyboard.TargetName="ScaleTransform" From="1" To="0" Duration="0:0:0.3">
                              <DoubleAnimation.EasingFunction>
                                <SineEase EasingMode="EaseOut" />
                              </DoubleAnimation.EasingFunction>
                            </DoubleAnimation>
                          </Storyboard>
                      </Storyboard>
                  </BeginStoryboard>
              </Trigger.ExitActions>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
              <Setter Property="Opacity" Value="{StaticResource DisabledOpacity}"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="MaterialDesignGroupBoxTabControl" BasedOn="{StaticResource MaterialDesignTabControl}" TargetType="{x:Type TabControl}">
    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground), FallbackValue={StaticResource MaterialDesignBody}}"/>
    <Setter Property="Background" Value="{DynamicResource PrimaryHueMidLightBrush}"/>
    <Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidLightForegroundBrush}"/>
    <Setter Property="ItemContainerStyle" Value="{StaticResource MaterialDesignGroupBoxTabItem}"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TabControl}">
          <Grid KeyboardNavigation.TabNavigation="Local" Margin="-2" ClipToBounds="True">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="0"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto"/>
              <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <wpf:ColorZone
              UseLayoutRounding="True"
              x:Name="PART_ColorZone"
              Padding="4,0,4,0"
              Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}"
              wpf:ShadowAssist.ShadowEdges="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowEdges)}"
              Background="{TemplateBinding Background}"
              Foreground="{TemplateBinding Foreground}"
              BorderBrush="{TemplateBinding BorderBrush}"
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
              <ScrollViewer VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" Template="{DynamicResource ScrollViewerTemplate}" Margin="0,0,0,-1">
                <DockPanel>
                  <StackPanel x:Name="HeaderPanel" DockPanel.Dock="Left" Orientation="Horizontal" IsItemsHost="True" Panel.ZIndex="1" KeyboardNavigation.TabIndex="1"/>
                </DockPanel>
              </ScrollViewer>
            </wpf:ColorZone>
            <ContentPresenter
              x:Name="PART_SelectedContentHost"
              Grid.Column="0"
              Grid.Row="1"
              ContentTemplate="{TemplateBinding SelectedContentTemplate}"
              Content="{TemplateBinding SelectedContent}"
              ContentStringFormat="{TemplateBinding SelectedContentStringFormat}"
              ContentSource="SelectedContent"
              Margin="{TemplateBinding Padding}"
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="{x:Type TabControl}" BasedOn="{StaticResource MaterialDesignTabControl}" TargetType="{x:Type TabControl}"/>

  <Style x:Key="{x:Type TabItem}" BasedOn="{StaticResource MaterialDesignTabItem}" TargetType="{x:Type TabItem}"/>

</ResourceDictionary>
